package com.chainyoung.component_tmperson.ui.activity.hdwallet;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chainyoung.common.appcommon.hdwallet.WalletStoreManger;
import com.chainyoung.common.appcommon.hdwallet.WordsListUtil;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.common.appcommon.hdwallet.OwnWalletUtils;
import com.chainyoung.component_tmperson.ui.activity.ScanQrCodeActivity;
import com.chainyoung.component_tmperson.ui.view.RestoreWordsRemindDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;
import io.github.novacrypto.bip39.wordlists.English;

import static com.chainyoung.common.appcommon.hdwallet.CoinConstant.ETH;


/**
 * 恢复助记词
 */
public class RestoreHdWalletActivity extends BaseActivity {


    @BindViews({R2.id.tv_index1, R2.id.tv_index2, R2.id.tv_index3, R2.id.tv_index4, R2.id.tv_index5, R2.id.tv_index6, R2.id.tv_index7, R2.id.tv_index8,
            R2.id.tv_index9, R2.id.tv_index10, R2.id.tv_index11, R2.id.tv_index12})
    List<TextView> textIndexs;

    @BindViews({R2.id.et_word1, R2.id.et_word2, R2.id.et_word3, R2.id.et_word4, R2.id.et_word5, R2.id.et_word6, R2.id.et_word7, R2.id.et_word8,
            R2.id.et_word9, R2.id.et_word10, R2.id.et_word11, R2.id.et_word12})
    List<EditText> editWordList;

    @BindView(R2.id.tv_save)
    TextView tvSave;


    private List<String> wordsList = new ArrayList<>();


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    protected void initUI() {
        setCurrentTitle("恢复助记词钱包");
        initIndexs();
        if (WalletStoreManger.getHdWallets() != null) {
            showRemindDiaLog();
        }

        Drawable drawable=ContextCompat.getDrawable(mContext,R.mipmap.ic_scan);
        DrawableCompat.setTint(drawable, Color.parseColor("#061c69"));
        showAndSetBarRightIcon(drawable);
        editWordList.get(0).clearFocus();

        setListeners();

    }

    private void setListeners() {
        for (final EditText editText : editWordList) {
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!WordsListUtil.wordIsValid(s.toString().trim())) {
                        editText.setTextColor(ContextCompat.getColor(mContext, R.color._F95A54));
                    } else {
                        editText.setTextColor(ContextCompat.getColor(mContext, R.color._061c69));
                    }
                }
            });
        }
    }


    private void initIndexs() {
        for (int i = 0; i < textIndexs.size(); i++) {
            textIndexs.get(i).setText(i + 1 + "");
        }
    }


    private RestoreWordsRemindDialog wordsRemindDialog;

    private void showRemindDiaLog() {
        wordsRemindDialog = new RestoreWordsRemindDialog(mContext, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.tv_cancel) {
                    wordsRemindDialog.close();
                    finish();
                } else if (v.getId() == R.id.tv_confirm) {
                    if (wordsRemindDialog.isAgree()) {
                        wordsRemindDialog.close();
                    }
                }
            }
        });
        wordsRemindDialog.show();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_restore_hdwallet;
    }


    private final static int REQUEST_WORDS = 0x1111;

    @Override
    public void onRightImgClicked(View v) {
        super.onRightImgClicked(v);
        launchForResult(ScanQrCodeActivity.class, REQUEST_WORDS);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        String words = data.getStringExtra("data");
        if (words.contains(":")) {
            words = words.substring(words.lastIndexOf(":") + 1);
        }
        wordsList.clear();
        try {
            wordsList.addAll(Arrays.asList(words.split(" ")));
        } catch (Exception ignore) {

        }
        if (wordsList.size() == 12) {
            restoreView();
        } else {
            ToastUtil.showToast("请扫描正确的助记词二维码");
        }

    }

    private void restoreView() {
        for (int i = 0; i < wordsList.size(); i++) {
            editWordList.get(i).setText(wordsList.get(i));
        }
    }


    @OnClick(R2.id.tv_save)
    void restore() {
        wordsList.clear();
        for (int i = 0; i < editWordList.size(); i++) {
            if (!TextUtils.isEmpty(editWordList.get(i).getText().toString().trim())) {
                wordsList.add(editWordList.get(i).getText().toString().trim());
            }
        }
        if (wordsList.size() < 12) {
            ToastUtil.showToast("请输入完整的助记词");
            return;
        }

        for (String word : wordsList) {
            if (!WordsListUtil.wordIsValid(word)) {
                ToastUtil.showToast("请输入正确助记词");
                return;
            }
        }

        OwnWalletUtils.importByMnemonic(mContext, SPUtil.getString(LibConstant.PIN_CODE), StringUtil.listToStringBySpace(wordsList), ETH);

    }


}
